package ui 
{
	import Base.Loader.SWFLoader;
	
	import Common.GameDefine;
	import Common.RenderManager;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLRequest;
	
	import map.MapGrid;

	/**
	 * ...
	 * @author ...
	 */
	public class MapUIHandler extends uiBase
	{
		private var mapID:int = 0;
		private var loader:Loader;
		private var bitmap:Bitmap = null;
		
		public function MapUIHandler() 
		{
			path = "map.swf";
			name = "MapUI";
		}
		
		public function onError( e:IOErrorEvent ):void
		{
			if ( bitmap )
			{
				uiMc.imgMc.removeChild( bitmap );
				bitmap = null;
			}
		}
		
		public function onSecurityError( e:SecurityErrorEvent ):void
		{
			if ( bitmap )
			{
				uiMc.imgMc.removeChild( bitmap );
				bitmap = null;
			}
		}
		
		public function onLoadTexture( e:Event ):void
		{
			bitmap = Bitmap( e.target.content );
			
			loader.contentLoaderInfo.removeEventListener( Event.COMPLETE , onLoadTexture );
			loader.contentLoaderInfo.removeEventListener( IOErrorEvent.IO_ERROR , onError );
			loader.contentLoaderInfo.removeEventListener( SecurityErrorEvent.SECURITY_ERROR , onSecurityError );
			loader = null;
			
			uiMc.imgMc.addChild( bitmap );
			
			updatePos();
		}
			
		
		public override function onInit( loader:SWFLoader ):void
		{
			super.onInit( loader );
			
			uiMc.closeButton.addEventListener( MouseEvent.CLICK , onClose );
			
			updateName();
			
			updatePos();
		}
		
		
		public function onClose( e:MouseEvent ):void
		{
			visible( false );
		}
		
		
		public override function resize():void
		{
			if ( uiMc )
			{
				uiMc.x = RenderManager.instance.stageWidth - uiMc.px - 15;
				uiMc.y = RenderManager.instance.stageHeight * 0.1 + uiMc.py;
			}
		}
		
		public function updateName():void
		{
			if ( !uiMc )
			{
				return;
			}
			
			uiMc.setName( MapManager.instance.activeMapData.name );
		}
		
		public function updatePos():void
		{
			if ( !uiMc )
			{
				return;
			}
			
			if ( GameManager.instance.MainPlayer.MapID != mapID )
			{
				mapID = GameManager.instance.MainPlayer.MapID;
				
				if ( loader )
				{
					loader.contentLoaderInfo.removeEventListener( Event.COMPLETE , onLoadTexture );
					loader.contentLoaderInfo.removeEventListener( IOErrorEvent.IO_ERROR , onError );
					loader.contentLoaderInfo.removeEventListener( SecurityErrorEvent.SECURITY_ERROR , onSecurityError );
					loader = null;
				}
				
				if ( bitmap )
				{
					uiMc.imgMc.removeChild( bitmap );
					bitmap = null;
				}
				
				loader = new Loader(); 
				loader.contentLoaderInfo.addEventListener( Event.COMPLETE , onLoadTexture );
				loader.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR , onError );
				loader.contentLoaderInfo.addEventListener( SecurityErrorEvent.SECURITY_ERROR , onSecurityError );
				loader.load( new URLRequest( GameDefine.PATH + GameDefine.PATH1 + "minimap/" + mapID.toString() + ".png" ) );
				
				return;
			}
			
			
			var grid:MapGrid = GameManager.instance.MainPlayer.MMapGrid;
			
			uiMc.setPos( grid.x , grid.y );
			uiMc.imgMc.x = -( grid.x * 4 + grid.y * 4 + 4 ) / 2 + 4;
			uiMc.imgMc.y = -( -grid.x * 3 + grid.y * 3 + 3 * ( MapManager.instance.activeMapData.y - 1 ) + 3 ) / 2 + 3;
			
			
		}
		
		public static var instance:MapUIHandler = null;
		public static function Instance():MapUIHandler
		{
			if ( instance == null )
			{
				instance = new MapUIHandler();
			}
			
			return instance;
		}
	}

}
